package view.aptc.so;

import java.io.File;

import java.io.FileInputStream;
import java.io.OutputStream;

import java.util.ArrayList;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import javax.faces.model.SelectItem;

import oracle.adf.model.BindingContext;

import oracle.binding.BindingContainer;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.input.RichInputFile;
import oracle.adf.view.rich.component.rich.input.RichSelectOrderShuttle;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.PopupCanceledEvent;

import oracle.adfinternal.view.faces.model.binding.FacesCtrlHierBinding;

import oracle.jbo.Row;

import oracle.jbo.RowSet;
import oracle.jbo.RowSetIterator;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;

import oracle.jbo.uicli.binding.JUCtrlRangeBinding;

import org.apache.myfaces.trinidad.event.SelectionEvent;
import org.apache.myfaces.trinidad.model.CollectionModel;
import org.apache.myfaces.trinidad.model.RowKeySet;
import org.apache.myfaces.trinidad.model.RowKeySetImpl;
import org.apache.myfaces.trinidad.model.UploadedFile;

import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;

import view.common.Utils;

public class sobean {
    private RichPopup newPopupVN;
    private RichPopup newPopupEN;
    private RichPopup newPopupDeleteVN;
    private RichPopup newPopupDeleteEN;
    private RichInputFile inputFileVN;
    private RichInputFile inputFileEN;
    private UploadedFile _fileVN;
    private UploadedFile _fileEN;
    private RichCommandButton buttonDownload;
    private static int operationType = 0; //0: CreateInsert, 1: Update
    private ArrayList<Integer> selectedItemsVN = new ArrayList<Integer>();
    private ArrayList<Integer> selectedItemsEN = new ArrayList<Integer>();
    private RichSelectOrderShuttle shuttleVN;
    private RichSelectOrderShuttle shuttleEN;
    private static String fileDeleted;
    private UIComponent iterVN;
    private UIComponent iterEN;
    private static boolean hasFile = false;
    private boolean fileExisted = false;
    private String resetview = null;
    private int pageNumberVN;
    private int pageNumberEN;
    private static final int RANGE_SIZE = 15;
    private RichSelectOrderShuttle selectOrderVn;
    private RichSelectOrderShuttle selectOrderEn;
    private List<String> selectVnValues;
    private List<String> selectEnValues;
    private RichTable tblVN;
    private RichTable tblEN;
    private String soDtVN;
    private String soDtEN;
    private String soGiayVN;
    private String soGiayEN;

    private String kieuSelectedValueVN;
    private SelectItem[] kieuElementsVN = null;
    private String dangSelectedValueVN;
    private SelectItem[] dangElementsVN = null;

    private String kieuSelectedValueEN;
    private SelectItem[] kieuElementsEN = null;
    private String dangSelectedValueEN;
    private SelectItem[] dangElementsEN = null;

    private static List<String> validExt =
        Arrays.asList("jpg", "jpeg", "gif", "png", "bmp");

    public sobean() {
        JUCtrlRangeBinding soViewVN = this.getSoViewVN();
        soViewVN.getIteratorBinding().getNavigatableRowIterator().scrollRange(0);

        JUCtrlRangeBinding soViewEN = this.getSoViewEN();
        soViewEN.getIteratorBinding().getNavigatableRowIterator().scrollRange(0);
    }

    public String getResetview() {
        if (Boolean.FALSE.equals(Utils.getElExpression("#{adfFacesContext.postback}"))) {
            DCIteratorBinding chuyenmucVNIter =
                Utils.getIteratorBinding("AptcChuyenmucView_VN1Iterator");
            chuyenmucVNIter.getViewObject().getViewCriteriaManager().clearViewCriterias();
            chuyenmucVNIter.executeQuery();
            DCIteratorBinding soChuyenmucIteratorVN =
                Utils.getIteratorBinding("AptcSoChuyenmucView_VN1Iterator");
            soChuyenmucIteratorVN.getViewObject().getViewCriteriaManager().clearViewCriterias();
            soChuyenmucIteratorVN.executeQuery();

            DCIteratorBinding chuyenmucENIter =
                Utils.getIteratorBinding("AptcChuyenmucView_EN1Iterator");
            chuyenmucENIter.getViewObject().getViewCriteriaManager().clearViewCriterias();
            chuyenmucENIter.executeQuery();
            DCIteratorBinding soChuyenmucIteratorEN =
                Utils.getIteratorBinding("AptcSoChuyenmucView_EN1Iterator");
            soChuyenmucIteratorEN.getViewObject().getViewCriteriaManager().clearViewCriterias();
            soChuyenmucIteratorEN.executeQuery();
        }
        return resetview;
    }

    public void getSotapchiVn() {
        DCIteratorBinding soVNIter =
            Utils.getIteratorBinding("AptcSoView_VN1Iterator");
        ViewObject vo = soVNIter.getViewObject();
        ViewCriteria vc =
            vo.getViewCriteriaManager().getViewCriteria("AptcSoView_VNCriteria");
        vo.ensureVariableManager().setVariableValue("varDang",
                                                    dangSelectedValueVN);
        vo.ensureVariableManager().setVariableValue("varKieu",
                                                    kieuSelectedValueVN);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("AptcSoView_VNCriteria");
        vo.getViewCriteriaManager().applyViewCriteria(vc);
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            vo.setCurrentRowAtRangeIndex(0);
        }
    }

    public void getChuyenmucVn() {
        BindingContainer bc =
            BindingContext.getCurrent().getCurrentBindingsEntry();
        FacesCtrlHierBinding listBinding =
            (FacesCtrlHierBinding)bc.get("AptcChuyenmucView_VN1");
        Map<String, SelectItem> mapSelectItems = new HashMap();
        //Lấy các ID từ bảng Chuyên mục
        for (int i = 0; i < listBinding.getRowIterator().getRowCount(); i++) {
            SelectItem si = new SelectItem();
            si.setValue(listBinding.getAttributeFromRow(i, "Id").toString());
            si.setLabel(listBinding.getAttributeFromRow(i,
                                                        "Tieude").toString());
            mapSelectItems.put(listBinding.getAttributeFromRow(i,
                                                               "Id").toString(),
                               si);
        }
        UISelectItems usi = new UISelectItems();
        usi.setValue(mapSelectItems.values());
        selectOrderVn.getChildren().clear();
        selectOrderVn.setValue(null);
        selectOrderVn.getChildren().add(usi);
    }

    public void getSotapchiEn() {
        DCIteratorBinding soENIter =
            Utils.getIteratorBinding("AptcSoView_EN1Iterator");
        ViewObject vo = soENIter.getViewObject();
        ViewCriteria vc =
            vo.getViewCriteriaManager().getViewCriteria("AptcSoView_ENCriteria");
        vo.ensureVariableManager().setVariableValue("varDang",
                                                    dangSelectedValueEN);
        vo.ensureVariableManager().setVariableValue("varKieu",
                                                    kieuSelectedValueEN);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("AptcSoView_ENCriteria");
        vo.getViewCriteriaManager().applyViewCriteria(vc);
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            vo.setCurrentRowAtRangeIndex(0);
        }
    }

    public void getChuyenmucEn() {
        BindingContainer bc =
            BindingContext.getCurrent().getCurrentBindingsEntry();
        FacesCtrlHierBinding listBinding =
            (FacesCtrlHierBinding)bc.get("AptcChuyenmucView_EN1");
        Map<String, SelectItem> mapSelectItems = new HashMap();
        //Lấy các ID từ bảng Chuyên mục
        for (int i = 0; i < listBinding.getRowIterator().getRowCount(); i++) {
            SelectItem si = new SelectItem();
            si.setValue(listBinding.getAttributeFromRow(i, "Id").toString());
            si.setLabel(listBinding.getAttributeFromRow(i,
                                                        "Tieude").toString());
            mapSelectItems.put(listBinding.getAttributeFromRow(i,
                                                               "Id").toString(),
                               si);
        }
        UISelectItems usi = new UISelectItems();
        usi.setValue(mapSelectItems.values());
        selectOrderEn.getChildren().clear();
        selectOrderEn.setValue(null);
        selectOrderEn.getChildren().add(usi);
    }

    public void createVN(ActionEvent actionEvent) {
        Utils.iteratorRefresh("AptcSoView_VN1Iterator");
        Utils.execute("CreateInsertVN");
        getChuyenmucVn();
        Utils.showPopup(newPopupVN);
    }

    public void createEN(ActionEvent actionEvent) {
        Utils.iteratorRefresh("AptcSoView_EN1Iterator");
        Utils.execute("CreateInsertEN");
        getChuyenmucEn();
        Utils.showPopup(newPopupEN);
    }

    public void updateVN(ActionEvent actionEvent) {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("AptcSoView_VN1Iterator");

        if (soIterator.getCurrentRow() == null) {
            String messageText = "Bạn chưa chọn số tạp chí nào.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        } else {
            String soId =
                soIterator.getCurrentRow().getAttribute("Id").toString();
            BindingContainer bc =
                BindingContext.getCurrent().getCurrentBindingsEntry();
            FacesCtrlHierBinding listBinding =
                (FacesCtrlHierBinding)bc.get("AptcChuyenmucView_VN1");
            DCIteratorBinding soChuyenmucIterator1 =
                Utils.getIteratorBinding("AptcSoChuyenmucView_VN1Iterator");
            ViewObject vo = soChuyenmucIterator1.getViewObject();
            vo.setRangeSize(-1);
            vo.executeQuery();
            Row[] row = vo.getAllRowsInRange();
            Map<String, Integer> mapSelected = new HashMap();

            ArrayList listSelectedItems = new ArrayList();
            Map<String, SelectItem> mapSelectItems = new HashMap();
            //Lấy các ID từ bảng Chuyên mục
            for (int i = 0; i < listBinding.getRowIterator().getRowCount();
                 i++) {
                mapSelected.put(listBinding.getAttributeFromRow(i,
                                                                "Id").toString(),
                                i);
                SelectItem si = new SelectItem();
                si.setValue(listBinding.getAttributeFromRow(i,
                                                            "Id").toString());
                si.setLabel(listBinding.getAttributeFromRow(i,
                                                            "Tieude").toString());
                mapSelectItems.put(listBinding.getAttributeFromRow(i,
                                                                   "Id").toString(),
                                   si);
            }

            //Lấy ID từ bảng Số Chuyên mục so sánh với ID của bảng Chuyên mục
            DCIteratorBinding soCmBaivietIterator1 =
                Utils.getIteratorBinding("AptcSoChuyenmucInBaiviet1Iterator");
            ViewObject vo2 = soCmBaivietIterator1.getViewObject();
            ViewCriteria vc2 = vo2.createViewCriteria();
            ViewCriteriaRow cr2 = vc2.createViewCriteriaRow();
            String varStr2 = "='vi'";
            cr2.setAttribute("NgonNgu", varStr2);
            vc2.addElement(cr2);
            vo2.getViewCriteriaManager().clearViewCriterias();
            vo2.applyViewCriteria(vc2);
            vo2.setRangeSize(-1);
            vo2.executeQuery();

            for (int j = 0; j < row.length; j++) {
                String chuyenmucId =
                    row[j].getAttribute("ChuyenmucId").toString();
                Integer index = mapSelected.get(chuyenmucId);
                if (index != null &&
                    soId.equals(row[j].getAttribute("AptcSoId").toString())) {
                    listSelectedItems.add(chuyenmucId);

                    if (vo2.getAllRowsInRange().length > 0) {
                        for (int k = 0; k < vo2.getAllRowsInRange().length;
                             k++) {
                            String cm =
                                vo2.getRowAtRangeIndex(k).getAttribute("ChuyenmucId").toString();
                            String so =
                                vo2.getRowAtRangeIndex(k).getAttribute("AptcSoId").toString();
                            if (cm.equals(chuyenmucId) && so.equals(soId)) {
                                mapSelectItems.get(chuyenmucId).setDescription("Có bài viết đang thuộc chuyện mục này.");
                                mapSelectItems.get(chuyenmucId).setDisabled(true);
                            }
                        }
                    }
                }
            }
            UISelectItems usi = new UISelectItems();
            usi.setValue(mapSelectItems.values());
            selectOrderVn.getChildren().clear();
            selectOrderVn.setValue(listSelectedItems);
            selectOrderVn.getChildren().add(usi);

            if (soIterator.getCurrentRow().getAttribute("FileId") != null) {
                hasFile = true;
                DCIteratorBinding fileIterator =
                    Utils.getIteratorBinding("HtFileView1Iterator");
                ViewObject vo1 = fileIterator.getViewObject();
                ViewCriteria vc = vo1.createViewCriteria();
                ViewCriteriaRow cr = vc.createViewCriteriaRow();
                String varStr =
                    "=" + soIterator.getCurrentRow().getAttribute("FileId");
                cr.setAttribute("Id", varStr);
                vc.addElement(cr);
                vo1.getViewCriteriaManager().clearViewCriterias();
                vo1.applyViewCriteria(vc);
                vo1.executeQuery();
            } else {
                hasFile = false;
            }
            this.operationType = 1;
            Utils.showPopup(newPopupVN);
        }
    }

    public void updateEN(ActionEvent actionEvent) {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("AptcSoView_EN1Iterator");

        if (soIterator.getCurrentRow() == null) {
            String messageText = "Bạn chưa chọn số tạp chí nào.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        } else {
            String soId =
                soIterator.getCurrentRow().getAttribute("Id").toString();
            BindingContainer bc =
                BindingContext.getCurrent().getCurrentBindingsEntry();
            FacesCtrlHierBinding listBinding =
                (FacesCtrlHierBinding)bc.get("AptcChuyenmucView_EN1");
            DCIteratorBinding soChuyenmucIterator1 =
                Utils.getIteratorBinding("AptcSoChuyenmucView_EN1Iterator");
            ViewObject vo = soChuyenmucIterator1.getViewObject();
            vo.setRangeSize(-1);
            vo.executeQuery();
            Row[] row = vo.getAllRowsInRange();
            Map<String, Integer> mapSelected = new HashMap();

            ArrayList listSelectedItems = new ArrayList();
            Map<String, SelectItem> mapSelectItems = new HashMap();
            //Lấy các ID từ bảng Chuyên mục
            for (int i = 0; i < listBinding.getRowIterator().getRowCount();
                 i++) {
                mapSelected.put(listBinding.getAttributeFromRow(i,
                                                                "Id").toString(),
                                i);
                SelectItem si = new SelectItem();
                si.setValue(listBinding.getAttributeFromRow(i,
                                                            "Id").toString());
                si.setLabel(listBinding.getAttributeFromRow(i,
                                                            "Tieude").toString());
                mapSelectItems.put(listBinding.getAttributeFromRow(i,
                                                                   "Id").toString(),
                                   si);
            }

            //Lấy ID từ bảng Số Chuyên mục so sánh với ID của bảng Chuyên mục
            DCIteratorBinding soCmBaivietIterator1 =
                Utils.getIteratorBinding("AptcSoChuyenmucInBaiviet1Iterator");
            ViewObject vo2 = soCmBaivietIterator1.getViewObject();
            ViewCriteria vc2 = vo2.createViewCriteria();
            ViewCriteriaRow cr2 = vc2.createViewCriteriaRow();
            String varStr2 = "='en'";
            cr2.setAttribute("NgonNgu", varStr2);
            vc2.addElement(cr2);
            vo2.getViewCriteriaManager().clearViewCriterias();
            vo2.applyViewCriteria(vc2);
            vo2.setRangeSize(-1);
            vo2.executeQuery();

            for (int j = 0; j < row.length; j++) {
                String chuyenmucId =
                    row[j].getAttribute("ChuyenmucId").toString();
                Integer index = mapSelected.get(chuyenmucId);
                if (index != null &&
                    soId.equals(row[j].getAttribute("AptcSoId").toString())) {
                    listSelectedItems.add(chuyenmucId);

                    if (vo2.getAllRowsInRange().length > 0) {
                        for (int k = 0; k < vo2.getAllRowsInRange().length;
                             k++) {
                            String cm =
                                vo2.getRowAtRangeIndex(k).getAttribute("ChuyenmucId").toString();
                            String so =
                                vo2.getRowAtRangeIndex(k).getAttribute("AptcSoId").toString();
                            if (cm.equals(chuyenmucId) && so.equals(soId)) {
                                mapSelectItems.get(chuyenmucId).setDisabled(true);
                            }
                        }
                    }
                }
            }
            UISelectItems usi = new UISelectItems();
            usi.setValue(mapSelectItems.values());
            selectOrderEn.getChildren().clear();
            selectOrderEn.setValue(listSelectedItems);
            selectOrderEn.getChildren().add(usi);

            if (soIterator.getCurrentRow().getAttribute("FileId") != null) {
                hasFile = true;
                DCIteratorBinding fileIterator =
                    Utils.getIteratorBinding("HtFileView1Iterator");
                ViewObject vo1 = fileIterator.getViewObject();
                ViewCriteria vc = vo1.createViewCriteria();
                ViewCriteriaRow cr = vc.createViewCriteriaRow();
                String varStr =
                    "=" + soIterator.getCurrentRow().getAttribute("FileId");
                cr.setAttribute("Id", varStr);
                vc.addElement(cr);
                vo1.getViewCriteriaManager().clearViewCriterias();
                vo1.applyViewCriteria(vc);
                vo1.executeQuery();
            } else {
                hasFile = false;
            }
            this.operationType = 1;
            Utils.showPopup(newPopupEN);
        }
    }

    /**
     * @param dialogEvent
     */
    public void commitVN(DialogEvent dialogEvent) {
        try {
            Boolean validated = true;
            File filePath =
                new File(Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator"),
                         Utils.ENUM_CAU_HINH_KHOA.APTC_DIR.getValue("HtCauHinhView1Iterator"));
            File fileDir =
                new File(Utils.ENUM_CAU_HINH_KHOA.APTC_DIR.getValue("HtCauHinhView1Iterator"));
            DCIteratorBinding soIteratorVN =
                Utils.getIteratorBinding("AptcSoView_VN1Iterator");
            Object fileId =
                soIteratorVN.getCurrentRow().getAttribute("FileId");
            if (_fileVN != null) {
                if (Utils.checkFileExtension(_fileVN.getFilename(),
                                             validExt)) {
                    if (this.operationType == 1) {
                        if (fileId == null) {
                            Utils.execute("HtFileInsert");
                            fileId =
                                    Utils.getIteratorBinding("HtFileView1Iterator").getCurrentRow().getAttribute("Id").toString();
                        }
                    } else {
                        Utils.execute("HtFileInsert");
                        fileId =
                                Utils.getIteratorBinding("HtFileView1Iterator").getCurrentRow().getAttribute("Id").toString();
                    }
                    if (Utils.imageUpload(_fileVN, filePath,
                                          fileId.toString())) {
                        Utils.processUpload(_fileVN, fileDir,
                                            "HtFileView1Iterator",
                                            "AptcSoView_VN1Iterator",
                                            this.operationType,
                                            Utils.ENUM_MA_UNG_DUNG.APTC,
                                            fileId.toString());
                    } else {
                        validated = false;
                        Utils.showMessage(Utils.MSG_ERROR,
                                          "Có lỗi xảy ra trong quá trình upload ảnh.");
                    }
                } else {
                    validated = false;
                    _fileVN = null;
                    inputFileVN.resetValue();
                    inputFileVN.setValue(null);
                    inputFileVN.setChanged(false);
                    inputFileVN.setSubmittedValue(null);
                    inputFileVN.setValid(false);
                    AdfFacesContext.getCurrentInstance().addPartialTarget(inputFileVN);
                    Utils.showMessage(Utils.MSG_ERROR,
                                      "Hệ thống chỉ hỗ trợ các định dạng ảnh .jpg, .jpeg, .gif, .png, .bmp");
                }
            }
            if (soIteratorVN.getAllRowsInRange().length > 0) {
                if (this.operationType == 1) {
                    if (soDtVN.equals(soIteratorVN.getCurrentRow().getAttribute("Sodientu").toString()) ==
                        false ||
                        soGiayVN.equals(soIteratorVN.getCurrentRow().getAttribute("Sogiay").toString()) ==
                        false) {
                        if (this.checkExisted(soDtVN, soGiayVN,
                                              "AptcSoValidate1Iterator",
                                              "vi")) {
                            validated = false;
                            Utils.showMessage(Utils.MSG_ERROR,
                                              "Số ấn phẩm bạn vừa nhập đã tồn tại trong hệ thống.");
                        }
                    }
                } else {
                    if (this.checkExisted(soDtVN, soGiayVN,
                                          "AptcSoValidate1Iterator", "vi")) {
                        validated = false;
                        Utils.showMessage(Utils.MSG_ERROR,
                                          "Số ấn phẩm bạn vừa nhập đã tồn tại trong hệ thống.");
                    }
                }
            }
            if (validated) {
                DCIteratorBinding soChuyenmucIterator1 =
                    Utils.getIteratorBinding("AptcSoChuyenmucView_VN1Iterator");
                ViewObject vo = soChuyenmucIterator1.getViewObject();
                vo.setRangeSize(-1);
                vo.executeQuery();
                String soId =
                    soIteratorVN.getCurrentRow().getAttribute("Id").toString();
                if (this.operationType ==
                    1) { //Xoa du lieu bang SoChuyenmuc theo So ID khi update
                    Row[] row = vo.getAllRowsInRange();
                    for (int j = 0; j < row.length; j++) {
                        if (soId.equals(row[j].getAttribute("AptcSoId").toString())) {
                            soChuyenmucIterator1.setCurrentRowWithKeyValue(row[j].getAttribute("Id").toString());
                            soChuyenmucIterator1.removeCurrentRow();
                        }
                    }
                }
                for (int i = 0; i < selectVnValues.size(); i++) {
                    Utils.execute("SoChuyenmucInsertVN");
                    soChuyenmucIterator1.getCurrentRow().setAttribute("AptcSoId",
                                                                      soId);
                    soChuyenmucIterator1.getCurrentRow().setAttribute("ChuyenmucId",
                                                                      selectVnValues.get(i));
                    soChuyenmucIterator1.getCurrentRow().setAttribute("Vitri",
                                                                      i + 1);
                    soChuyenmucIterator1.getCurrentRow().setAttribute("NgonNgu",
                                                                      "vi");
                }
                if (fileId != null) {
                    if (fileId.toString().equals(fileDeleted)) {
                        soIteratorVN.getCurrentRow().setAttribute("FileId",
                                                                  null);
                    }
                }
                soIteratorVN.getCurrentRow().setAttribute("Sodientu", soDtVN);
                soIteratorVN.getCurrentRow().setAttribute("Sogiay", soGiayVN);
                soIteratorVN.getCurrentRow().setAttribute("NgonNgu", "vi");
                soIteratorVN.getCurrentRow().setAttribute("NguoiNhap",
                                                          Utils.getCurrentUser());
                soIteratorVN.getCurrentRow().setAttribute("NgayNhap",
                                                          Utils.getCurrentDate());
                Utils.execute("Commit");
                Utils.iteratorRefresh("AptcSoView_VN1Iterator");
                int prePage =
                    getSoViewVN().getIteratorBinding().getNavigatableRowIterator().getRangeStart() /
                    RANGE_SIZE + 1;
                getSoViewVN().getIteratorBinding().getNavigatableRowIterator().scrollRange(RANGE_SIZE *
                                                                                           (pageNumberVN -
                                                                                            prePage));
                soIteratorVN.setCurrentRowWithKeyValue(soId);
                this.operationType = 0;
                fileDeleted = "";
                AdfFacesContext.getCurrentInstance().addPartialTarget(tblVN);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //Lay so thu tu bang cach set nguoc lai thang hashmap
        }
    }

    /**
     * @param dialogEvent
     */
    public void commitEN(DialogEvent dialogEvent) {
        try {
            Boolean validated = true;
            File filePath =
                new File(Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator"),
                         Utils.ENUM_CAU_HINH_KHOA.APTC_DIR.getValue("HtCauHinhView1Iterator"));
            File fileDir =
                new File(Utils.ENUM_CAU_HINH_KHOA.APTC_DIR.getValue("HtCauHinhView1Iterator"));
            DCIteratorBinding soIteratorEN =
                Utils.getIteratorBinding("AptcSoView_EN1Iterator");
            Object fileId =
                soIteratorEN.getCurrentRow().getAttribute("FileId");
            if (_fileEN != null) {
                //Check file extensions
                if (Utils.checkFileExtension(_fileEN.getFilename(),
                                             validExt)) {
                    if (this.operationType == 1) {
                        if (fileId == null) {
                            Utils.execute("HtFileInsert");
                            fileId =
                                    Utils.getIteratorBinding("HtFileView1Iterator").getCurrentRow().getAttribute("Id").toString();
                        }
                    } else {
                        Utils.execute("HtFileInsert");
                        fileId =
                                Utils.getIteratorBinding("HtFileView1Iterator").getCurrentRow().getAttribute("Id");
                    }
                    if (Utils.imageUpload(_fileEN, filePath,
                                          fileId.toString())) {
                        Utils.processUpload(_fileEN, fileDir,
                                            "HtFileView1Iterator",
                                            "AptcSoView_EN1Iterator",
                                            this.operationType,
                                            Utils.ENUM_MA_UNG_DUNG.APTC,
                                            fileId.toString());
                    } else {
                        validated = false;
                        String msg =
                            "Có lỗi xảy ra trong quá trình upload ảnh.";
                        Utils.showMessage(Utils.MSG_ERROR, msg);
                    }
                } else {
                    validated = false;
                    _fileEN = null;
                    inputFileEN.resetValue();
                    inputFileEN.setValue(null);
                    inputFileEN.setChanged(false);
                    inputFileEN.setSubmittedValue(null);
                    inputFileEN.setValid(false);
                    AdfFacesContext.getCurrentInstance().addPartialTarget(inputFileEN);
                    Utils.showMessage(Utils.MSG_ERROR,
                                      "Hệ thống chỉ hỗ trợ các định dạng ảnh .jpg, .jpeg, .gif, .png, .bmp");
                }
            }
            if (soIteratorEN.getAllRowsInRange().length > 0) {
                if (this.operationType == 1) {
                    if (soDtEN.equals(soIteratorEN.getCurrentRow().getAttribute("Sodientu").toString()) ==
                        false ||
                        soGiayEN.equals(soIteratorEN.getCurrentRow().getAttribute("Sogiay").toString()) ==
                        false) {
                        if (this.checkExisted(soDtEN, soGiayEN,
                                              "AptcSoValidate1Iterator",
                                              "en")) {
                            validated = false;
                            Utils.showMessage(Utils.MSG_ERROR,
                                              "Số tạp chí bạn vừa nhập đã tồn tại trong hệ thống.");
                        }
                    }
                } else {
                    if (this.checkExisted(soDtEN, soGiayEN,
                                          "AptcSoValidate1Iterator", "en")) {
                        validated = false;
                        Utils.showMessage(Utils.MSG_ERROR,
                                          "Số tạp chí bạn vừa nhập đã tồn tại trong hệ thống.");
                    }
                }
            }
            if (validated) {
                DCIteratorBinding soChuyenmucIterator1 =
                    Utils.getIteratorBinding("AptcSoChuyenmucView_EN1Iterator");
                ViewObject vo = soChuyenmucIterator1.getViewObject();
                vo.setRangeSize(-1);
                vo.executeQuery();
                String soId =
                    soIteratorEN.getCurrentRow().getAttribute("Id").toString();
                if (this.operationType ==
                    1) { //Xoa du lieu bang SoChuyenmuc theo So ID khi update
                    Row[] row = vo.getAllRowsInRange();
                    for (int j = 0; j < row.length; j++) {
                        if (soId.equals(row[j].getAttribute("AptcSoId").toString())) {
                            soChuyenmucIterator1.setCurrentRowWithKeyValue(row[j].getAttribute("Id").toString());
                            soChuyenmucIterator1.removeCurrentRow();
                        }
                    }
                }
                for (int i = 0; i < selectEnValues.size(); i++) {
                    Utils.execute("SoChuyenmucInsertEN");
                    soChuyenmucIterator1.getCurrentRow().setAttribute("AptcSoId",
                                                                      soId);
                    soChuyenmucIterator1.getCurrentRow().setAttribute("ChuyenmucId",
                                                                      selectEnValues.get(i));
                    soChuyenmucIterator1.getCurrentRow().setAttribute("Vitri",
                                                                      i + 1);
                    soChuyenmucIterator1.getCurrentRow().setAttribute("NgonNgu",
                                                                      "en");
                }
                if (fileId != null) {
                    if (fileId.toString().equals(fileDeleted)) {
                        soIteratorEN.getCurrentRow().setAttribute("FileId",
                                                                  null);
                    }
                }
                soIteratorEN.getCurrentRow().setAttribute("Sodientu", soDtEN);
                soIteratorEN.getCurrentRow().setAttribute("Sogiay", soGiayEN);
                soIteratorEN.getCurrentRow().setAttribute("NgonNgu", "en");
                soIteratorEN.getCurrentRow().setAttribute("NguoiNhap",
                                                          Utils.getCurrentUser());
                soIteratorEN.getCurrentRow().setAttribute("NgayNhap",
                                                          Utils.getCurrentDate());
                Utils.execute("Commit");
                Utils.iteratorRefresh("AptcSoView_EN1Iterator");
                int prePage =
                    getSoViewEN().getIteratorBinding().getNavigatableRowIterator().getRangeStart() /
                    RANGE_SIZE + 1;
                getSoViewEN().getIteratorBinding().getNavigatableRowIterator().scrollRange(RANGE_SIZE *
                                                                                           (pageNumberEN -
                                                                                            prePage));
                soIteratorEN.setCurrentRowWithKeyValue(soId);
                this.operationType = 0;
                fileDeleted = "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            //Lay so thu tu bang +cach set nguoc lai thang hashmap
        }
    }

    public static Boolean checkExisted(String sodt, String sogiay,
                                       String iterName, String lang) {
        DCIteratorBinding viewIterator = Utils.getIteratorBinding(iterName);
        if (viewIterator.getCurrentRow() != null) {
            ViewObject vo = viewIterator.getViewObject();
            ViewCriteria vc =
                vo.getViewCriteriaManager().getViewCriteria("AptcSoValidateCriteria");
            vo.ensureVariableManager().setVariableValue("varSoDt", sodt);
            vo.ensureVariableManager().setVariableValue("varSogiay", sogiay);
            vo.ensureVariableManager().setVariableValue("varLang", lang);
            vo.getViewCriteriaManager().removeApplyViewCriteriaName("AptcSoValidateCriteria");
            vo.getViewCriteriaManager().applyViewCriteria(vc);
            vo.setRangeSize(-1);
            vo.executeQuery();
            if (vo.getAllRowsInRange().length > 0) {
                return true;
            }
        }
        return false;
    }

    public void cancelDiaglog(PopupCanceledEvent popupCanceledEvent) {
        if (this.operationType == 0) {
            Utils.execute("Rollback");
        }
        if (operationType == 1) {
            Utils.iteratorRefresh("HtFileView1Iterator");
            hasFile = false;
        }
        this.operationType = 0;
    }

    public void deleteVN(DialogEvent dialogEvent) {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("AptcSoView_VN1Iterator");
        String soId = soIterator.getCurrentRow().getAttribute("Id").toString();
        ViewObject vo = soIterator.getViewObject();
        vo.setAccessMode(RowSet.SCROLLABLE);
        soIterator.setCurrentRowWithKeyValue(soId);

        DCIteratorBinding soChuyenmucIterator1 =
            Utils.getIteratorBinding("AptcSoChuyenmucView_VN1Iterator");
        ViewObject vo1 = soChuyenmucIterator1.getViewObject();
        vo1.clearCache();
        vo1.setRangeSize(-1);
        vo1.executeQuery();
        Row[] row = vo1.getAllRowsInRange();
        for (int j = 0; j < row.length; j++) {
            if (soId.equals(row[j].getAttribute("AptcSoId").toString())) {
                soChuyenmucIterator1.setCurrentRowWithKeyValue(row[j].getAttribute("Id").toString());
                soChuyenmucIterator1.removeCurrentRow();
            }
        }
        Utils.execute("SoChuyenmucDeleteVN");
        Utils.execute("DeleteVN");
        List constrainExist = Utils.execute("Commit");
        if (constrainExist != null &&
            constrainExist.toString().contains("APTC_BAIVIET_APTC_SO_FK")) {
            String messageText =
                "Có bài viết đang thuộc số tạp chí này. Bạn không thể xóa số tạp chí này.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        }
        vo.setAccessMode(RowSet.RANGE_PAGING);
        //Utils.iteratorRefresh("AptcSoView_VN1Iterator");
    }

    public void deleteEN(DialogEvent dialogEvent) {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("AptcSoView_EN1Iterator");
        String soId = soIterator.getCurrentRow().getAttribute("Id").toString();
        ViewObject vo = soIterator.getViewObject();
        vo.setAccessMode(RowSet.SCROLLABLE);
        soIterator.setCurrentRowWithKeyValue(soId);

        DCIteratorBinding soChuyenmucIterator1 =
            Utils.getIteratorBinding("AptcSoChuyenmucView_EN1Iterator");
        ViewObject vo1 = soChuyenmucIterator1.getViewObject();
        vo1.clearCache();
        vo1.setRangeSize(-1);
        vo1.executeQuery();
        Row[] row = vo1.getAllRowsInRange();
        for (int j = 0; j < row.length; j++) {
            if (soId.equals(row[j].getAttribute("AptcSoId").toString())) {
                soChuyenmucIterator1.setCurrentRowWithKeyValue(row[j].getAttribute("Id").toString());
                soChuyenmucIterator1.removeCurrentRow();
            }
        }
        Utils.execute("SoChuyenmucDeleteEN");
        Utils.execute("DeleteEN");
        List constrainExist = Utils.execute("Commit");
        if (constrainExist != null &&
            constrainExist.toString().contains("APTC_BAIVIET_APTC_SO_FK")) {
            String messageText =
                "Có bài viết đang thuộc số tạp chí này. Bạn không thể xóa số tạp chí này.";
            Utils.showMessage(Utils.MSG_ERROR, messageText);
        }
        vo.setAccessMode(RowSet.RANGE_PAGING);
        //Utils.iteratorRefresh("AptcSoView_EN1Iterator");
    }

    public static void setOperationType(int operationType) {
        sobean.operationType = operationType;
    }

    public static int getOperationType() {
        return operationType;
    }

    public void deleteFileVN(ActionEvent actionEvent) {
        // Add event code here...
        DCIteratorBinding fileIterator =
            Utils.getIteratorBinding("HtFileView1Iterator");
        fileIterator.setCurrentRowWithKeyValue(fileDeleted);
        fileIterator.removeCurrentRow();
        AdfFacesContext.getCurrentInstance().addPartialTarget(iterVN);
    }

    public void deleteFileEN(ActionEvent actionEvent) {
        // Add event code here...
        DCIteratorBinding fileIterator =
            Utils.getIteratorBinding("HtFileView1Iterator");
        fileIterator.setCurrentRowWithKeyValue(fileDeleted);
        fileIterator.removeCurrentRow();
        AdfFacesContext.getCurrentInstance().addPartialTarget(iterEN);
    }

    public void setNewPopupVN(RichPopup newPopupVN) {
        this.newPopupVN = newPopupVN;
    }

    public RichPopup getNewPopupVN() {
        return newPopupVN;
    }

    public void setNewPopupEN(RichPopup newPopupEN) {
        this.newPopupEN = newPopupEN;
    }

    public RichPopup getNewPopupEN() {
        return newPopupEN;
    }

    public void setFileVN(UploadedFile _fileVN) {
        this._fileVN = _fileVN;
    }

    public UploadedFile getFileVN() {
        return _fileVN;
    }

    public void setFileEN(UploadedFile _fileEN) {
        this._fileEN = _fileEN;
    }

    public UploadedFile getFileEN() {
        return _fileEN;
    }

    public void setSelectedItemsVN(ArrayList<Integer> selectedItemsVN) {
        this.selectedItemsVN = selectedItemsVN;
    }

    public ArrayList<Integer> getSelectedItemsVN() {
        return selectedItemsVN;
    }

    public void setSelectedItemsEN(ArrayList<Integer> selectedItemsEN) {
        this.selectedItemsEN = selectedItemsEN;
    }

    public ArrayList<Integer> getSelectedItemsEN() {
        return selectedItemsEN;
    }

    public void setShuttleVN(RichSelectOrderShuttle shuttleVN) {
        this.shuttleVN = shuttleVN;
    }

    public RichSelectOrderShuttle getShuttleVN() {
        return shuttleVN;
    }

    public void setShuttleEN(RichSelectOrderShuttle shuttleEN) {
        this.shuttleEN = shuttleEN;
    }

    public RichSelectOrderShuttle getShuttleEN() {
        return shuttleEN;
    }

    public void setFileDeleted(String fileDeleted) {
        this.fileDeleted = fileDeleted;
    }

    public String getFileDeleted() {
        return fileDeleted;
    }

    public static void setHasFile(boolean hasFile) {
        sobean.hasFile = hasFile;
    }

    public static boolean isHasFile() {
        return hasFile;
    }

    public void setFileExisted(boolean fileExisted) {
        this.fileExisted = fileExisted;
    }

    public boolean isFileExisted() {
        if (operationType == 1 && hasFile == true) {
            fileExisted = true;
        }
        return fileExisted;
    }

    public void setIterVN(UIComponent iterVN) {
        this.iterVN = iterVN;
    }

    public UIComponent getIterVN() {
        return iterVN;
    }

    public void setIterEN(UIComponent iterEN) {
        this.iterEN = iterEN;
    }

    public UIComponent getIterEN() {
        return iterEN;
    }

    public void rowSelectedVN(SelectionEvent se) {
        Utils.rowSelected(se, "AptcSoView_VN1");
    }

    public String goToPageVN() {
        Utils.goToPage(RANGE_SIZE, pageNumberVN, "AptcSoView_VN1");
        return null;
    }

    public String goLastPageVN() {
        Utils.goLastPage(RANGE_SIZE, "AptcSoView_VN1");
        return null;
    }

    public boolean isLastEnabledVN() {
        return Utils.isLastEnabled(RANGE_SIZE, "AptcSoView_VN1");
    }

    public boolean isFirstEnabledVN() {
        return Utils.isFirstEnabled(RANGE_SIZE, "AptcSoView_VN1");
    }

    private JUCtrlRangeBinding getSoViewVN() {
        return Utils.getViewObject("AptcSoView_VN1");
    }

    public void setPageNumberVN(int pageNumberVN) {
        JUCtrlRangeBinding soView = this.getSoViewVN();
        if (pageNumberVN >
            Math.max(1, (soView.getIteratorBinding().getEstimatedRowCount() -
                         1) / RANGE_SIZE + 1)) {
            pageNumberVN =
                    (int)Math.max(1, (soView.getIteratorBinding().getEstimatedRowCount() -
                                      1) / RANGE_SIZE + 1);
        }
        this.pageNumberVN = pageNumberVN;
    }

    public int getPageNumberVN() {
        return pageNumberVN =
                getSoViewVN().getIteratorBinding().getNavigatableRowIterator().getRangeStart() /
                RANGE_SIZE + 1;
    }

    //Paging for English

    public void rowSelectedEN(SelectionEvent se) {
        Utils.rowSelected(se, "AptcSoView_EN1");
    }

    public String goToPageEN() {
        Utils.goToPage(RANGE_SIZE, pageNumberEN, "AptcSoView_EN1");
        return null;
    }

    public String goLastPageEN() {
        Utils.goLastPage(RANGE_SIZE, "AptcSoView_EN1");
        return null;
    }

    public boolean isLastEnabledEN() {
        return Utils.isLastEnabled(RANGE_SIZE, "AptcSoView_EN1");
    }

    public boolean isFirstEnabledEN() {
        return Utils.isFirstEnabled(RANGE_SIZE, "AptcSoView_EN1");
    }

    private JUCtrlRangeBinding getSoViewEN() {
        return Utils.getViewObject("AptcSoView_EN1");
    }

    public void setPageNumberEN(int pageNumberEN) {
        JUCtrlRangeBinding soView = this.getSoViewEN();
        if (pageNumberEN >
            Math.max(1, (soView.getIteratorBinding().getEstimatedRowCount() -
                         1) / RANGE_SIZE + 1)) {
            pageNumberEN =
                    (int)Math.max(1, (soView.getIteratorBinding().getEstimatedRowCount() -
                                      1) / RANGE_SIZE + 1);
        }
        this.pageNumberEN = pageNumberEN;
    }

    public int getPageNumberEN() {
        return pageNumberEN =
                getSoViewEN().getIteratorBinding().getNavigatableRowIterator().getRangeStart() /
                RANGE_SIZE + 1;
    }

    public void dangtcVNValueChanged(ValueChangeEvent valueChangeEvent) {
        Utils.setElExpression("#{bindings.DangTapChiView_VN1.inputValue}",
                              valueChangeEvent.getNewValue());
    }

    public void kieuVNValueChanged(ValueChangeEvent valueChangeEvent) {
        Utils.setElExpression("#{bindings.AptcKieuView_VN1.inputValue}",
                              valueChangeEvent.getNewValue());
    }

    public void dangtcENValueChanged(ValueChangeEvent valueChangeEvent) {
        Utils.setElExpression("#{bindings.DangTapChiView_EN1.inputValue}",
                              valueChangeEvent.getNewValue());
    }

    public void kieuENValueChanged(ValueChangeEvent valueChangeEvent) {
        Utils.setElExpression("#{bindings.AptcKieuView_EN1.inputValue}",
                              valueChangeEvent.getNewValue());
    }

    public String soViewVNSearch() {
        getSotapchiVn();
        return null;
    }

    public String soViewENSearch() {
        getSotapchiEn();
        return null;
    }

    public void setSelectOrderVn(RichSelectOrderShuttle selectOrderVn) {
        this.selectOrderVn = selectOrderVn;
    }

    public RichSelectOrderShuttle getSelectOrderVn() {
        return selectOrderVn;
    }

    public void setSelectVnValues(List<String> selectVnValues) {
        this.selectVnValues = selectVnValues;
    }

    public List<String> getSelectVnValues() {
        return selectVnValues;
    }

    public void setSelectOrderEn(RichSelectOrderShuttle selectOrderEn) {
        this.selectOrderEn = selectOrderEn;
    }

    public RichSelectOrderShuttle getSelectOrderEn() {
        return selectOrderEn;
    }

    public void setSelectEnValues(List<String> selectEnValues) {
        this.selectEnValues = selectEnValues;
    }

    public List<String> getSelectEnValues() {
        return selectEnValues;
    }

    public void setTblVN(RichTable tblVN) {
        this.tblVN = tblVN;
    }

    public RichTable getTblVN() {
        return tblVN;
    }

    public void setTblEN(RichTable tblEN) {
        this.tblEN = tblEN;
    }

    public RichTable getTblEN() {
        return tblEN;
    }

    public void setSoDtVN(String soDtVN) {
        this.soDtVN = soDtVN;
    }

    public String getSoDtVN() {
        if (operationType == 1) {
            DCIteratorBinding soIterator =
                Utils.getIteratorBinding("AptcSoView_VN1Iterator");
            if (soIterator.getAllRowsInRange().length > 0) {
                soDtVN =
                        soIterator.getCurrentRow().getAttribute("Sodientu").toString();
            }
        }
        return soDtVN;
    }

    public void setSoDtEN(String soDtEN) {
        this.soDtEN = soDtEN;
    }

    public String getSoDtEN() {
        if (operationType == 1) {
            DCIteratorBinding soIterator =
                Utils.getIteratorBinding("AptcSoView_EN1Iterator");
            if (soIterator.getAllRowsInRange().length > 0) {
                soDtEN =
                        soIterator.getCurrentRow().getAttribute("Sodientu").toString();
            }
        }
        return soDtEN;
    }

    public void setSoGiayVN(String soGiayVN) {
        this.soGiayVN = soGiayVN;
    }

    public String getSoGiayVN() {
        if (operationType == 1) {
            DCIteratorBinding soIterator =
                Utils.getIteratorBinding("AptcSoView_VN1Iterator");
            if (soIterator.getAllRowsInRange().length > 0) {
                soGiayVN =
                        soIterator.getCurrentRow().getAttribute("Sogiay").toString();
            }
        }
        return soGiayVN;
    }

    public void setSoGiayEN(String soGiayEN) {
        this.soGiayEN = soGiayEN;
    }

    public String getSoGiayEN() {
        if (operationType == 1) {
            DCIteratorBinding soIterator =
                Utils.getIteratorBinding("AptcSoView_EN1Iterator");
            if (soIterator.getAllRowsInRange().length > 0) {
                soGiayEN =
                        soIterator.getCurrentRow().getAttribute("Sogiay").toString();
            }
        }
        return soGiayEN;
    }

    public void setNewPopupDeleteVN(RichPopup newPopupDeleteVN) {
        this.newPopupDeleteVN = newPopupDeleteVN;
    }

    public RichPopup getNewPopupDeleteVN() {
        return newPopupDeleteVN;
    }

    public void setNewPopupDeleteEN(RichPopup newPopupDeleteEN) {
        this.newPopupDeleteEN = newPopupDeleteEN;
    }

    public RichPopup getNewPopupDeleteEN() {
        return newPopupDeleteEN;
    }

    public void openDeletePopupVN(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("AptcSoView_VN1Iterator").getCurrentRow() ==
            null) {
            Utils.showMessage(Utils.MSG_ERROR,
                              "Bạn chưa chọn số tạp chí nào.");
        } else {
            Utils.showPopup(newPopupDeleteVN);
        }
    }

    public void openDeletePopupEN(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("AptcSoView_EN1Iterator").getCurrentRow() ==
            null) {
            Utils.showMessage(Utils.MSG_ERROR,
                              "Bạn chưa chọn số tạp chí nào.");
        } else {
            Utils.showPopup(newPopupDeleteEN);
        }
    }

    public void setKieuSelectedValueVN(String kieuSelectedValueVN) {
        this.kieuSelectedValueVN = kieuSelectedValueVN;
    }

    public String getKieuSelectedValueVN() {
        return kieuSelectedValueVN;
    }

    public void setKieuElementsVN(SelectItem[] kieuElementsVN) {
        this.kieuElementsVN = kieuElementsVN;
    }

    public SelectItem[] getKieuElementsVN() {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("AptcKieuView_VN1Iterator");
        int length = soIterator.getAllRowsInRange().length;
        kieuElementsVN = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = soIterator.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("Id").toString());
                item.setLabel(row.getAttribute("Tieude").toString());
            }
            kieuElementsVN[i] = item;
        }
        return kieuElementsVN;
    }

    public void setDangSelectedValueVN(String dangSelectedValueVN) {
        this.dangSelectedValueVN = dangSelectedValueVN;
    }

    public String getDangSelectedValueVN() {
        return dangSelectedValueVN;
    }

    public void setDangElementsVN(SelectItem[] dangElementsVN) {
        this.dangElementsVN = dangElementsVN;
    }

    public SelectItem[] getDangElementsVN() {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("DangTapChiView_VN1Iterator");
        int length = soIterator.getAllRowsInRange().length;
        dangElementsVN = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = soIterator.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("id").toString());
                item.setLabel(row.getAttribute("text").toString());
            }
            dangElementsVN[i] = item;
        }
        return dangElementsVN;
    }

    public void setKieuSelectedValueEN(String kieuSelectedValueEN) {
        this.kieuSelectedValueEN = kieuSelectedValueEN;
    }

    public String getKieuSelectedValueEN() {
        return kieuSelectedValueEN;
    }

    public void setKieuElementsEN(SelectItem[] kieuElementsEN) {
        this.kieuElementsEN = kieuElementsEN;
    }

    public SelectItem[] getKieuElementsEN() {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("AptcKieuView_EN1Iterator");
        int length = soIterator.getAllRowsInRange().length;
        kieuElementsEN = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = soIterator.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("Id").toString());
                item.setLabel(row.getAttribute("Tieude").toString());
            }
            kieuElementsEN[i] = item;
        }
        return kieuElementsEN;
    }

    public void setDangSelectedValueEN(String dangSelectedValueEN) {
        this.dangSelectedValueEN = dangSelectedValueEN;
    }

    public String getDangSelectedValueEN() {
        return dangSelectedValueEN;
    }

    public void setDangElementsEN(SelectItem[] dangElementsEN) {
        this.dangElementsEN = dangElementsEN;
    }

    public SelectItem[] getDangElementsEN() {
        DCIteratorBinding soIterator =
            Utils.getIteratorBinding("DangTapChiView_EN1Iterator");
        int length = soIterator.getAllRowsInRange().length;
        dangElementsEN = new SelectItem[length];
        SelectItem item = null;
        for (int i = 0; i < length; i++) {
            Row row = soIterator.getRowAtRangeIndex(i);
            if (row != null) {
                item = new SelectItem();
                item.setValue(row.getAttribute("id").toString());
                item.setLabel(row.getAttribute("text").toString());
            }
            dangElementsEN[i] = item;
        }
        return dangElementsEN;
    }

    public void setInputFileVN(RichInputFile inputFileVN) {
        this.inputFileVN = inputFileVN;
    }

    public RichInputFile getInputFileVN() {
        return inputFileVN;
    }

    public void setInputFileEN(RichInputFile inputFileEN) {
        this.inputFileEN = inputFileEN;
    }

    public RichInputFile getInputFileEN() {
        return inputFileEN;
    }

    public void setButtonDownload(RichCommandButton buttonDownload) {
        this.buttonDownload = buttonDownload;
    }

    public RichCommandButton getButtonDownload() {
        return buttonDownload;
    }

    public void downloadfile(ActionEvent actionEvent) {
        String fileName =
            (String)actionEvent.getComponent().getAttributes().get("fileName");
        buttonDownload.setText(fileName);
        FacesContext context = FacesContext.getCurrentInstance();
        ExtendedRenderKitService erks =
            Service.getService(context.getRenderKit(),
                               ExtendedRenderKitService.class);
        erks.addScript(context, "customHandler();");
    }

    public void dwdFile(FacesContext facesContext, OutputStream out) {
        File duongdan =
            new File(Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator"),
                     Utils.ENUM_CAU_HINH_KHOA.APTC_DIR.getValue("HtCauHinhView1Iterator"));
        File file = new File(duongdan + "/" + buttonDownload.getText());
        FileInputStream Fileinput = null;
        byte[] bt;
        try {
            Fileinput = new FileInputStream(file);
            int checkline;
            while ((checkline = Fileinput.available()) > 0) {
                bt = new byte[checkline];
                int rst = Fileinput.read(bt);
                out.write(bt, 0, bt.length);
                if (rst == -1)
                    break;

            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
